package com.qys.livesMall.system.service.impl;

import com.qys.livesMall.system.model.dto.sys.country.CountryEdit;
import com.qys.livesMall.system.model.dto.sys.country.CountrySearch;
import com.qys.livesMall.system.model.entity.Country;
import com.qys.livesMall.system.mapper.CountryMapper;
import com.qys.livesMall.system.service.CountryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.common.exception.enums.ExceptionEnum;
import com.qys.livesMall.framework.view.Paging;
import com.qys.livesMall.framework.view.QysPage;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author
 * @since 2023-08-28
 */
@Service
public class CountryServiceImpl extends ServiceImpl<CountryMapper, Country> implements CountryService {

    @Autowired
    private CountryMapper countryMapper;

    @Override
    public QysPage<Country> search(CountrySearch search) {
        Paging<Country> page=countryMapper.search(new Paging<>(search));
        if (CollectionUtils.isEmpty(page.getRecords())){
            return QysPage.build(search);
        }
        return QysPage.to(page);
    }

    @Override
    public void edit(CountryEdit country){
        if (country.getId()==null){
            throw new QysException(ExceptionEnum.PARAM_IS_ERROR);
        }
        Country up=new Country();
        up.setId(country.getId());
        up.setExchangeRate(country.getExchangeRate());
        if (StringUtils.isNotBlank(country.getCurrency())) {
            up.setCurrency(country.getCurrency());
        }
        updateById(up);
    }
}
